﻿#region << 版 本 注 释 >>

/*----------------------------------------------------------------
* 类 名 称 ：RectTransformExtension
* 类 描 述 ：
* 作    者 ：henrychang
* 创建时间 ：2019/1/6 20:18:50
*******************************************************************
* Copyright @ henrychang 2019. All rights reserved.
*******************************************************************
//----------------------------------------------------------------*/

#endregion << 版 本 注 释 >>

using UnityEngine;

public static class RectTransformExtension
{
    public static Vector2 GetPosInRootTrans(this RectTransform selfRectTransform, Transform rootTrans)
    {
        return RectTransformUtility.CalculateRelativeRectTransformBounds(rootTrans, selfRectTransform).center;
    }

    public static RectTransform AnchorPosX(this RectTransform selfRectTrans, float anchorPosX)
    {
        var anchorPos = selfRectTrans.anchoredPosition;
        anchorPos.x = anchorPosX;
        selfRectTrans.anchoredPosition = anchorPos;
        return selfRectTrans;
    }

    public static RectTransform AnchorPosY(this RectTransform selfRectTrans, float anchorPosY)
    {
        var anchorPos = selfRectTrans.anchoredPosition;
        anchorPos.y = anchorPosY;
        selfRectTrans.anchoredPosition = anchorPos;
        return selfRectTrans;
    }

    public static RectTransform SetSizeWidth(this RectTransform selfRectTrans, float sizeWidth)
    {
        var sizeDelta = selfRectTrans.sizeDelta;
        sizeDelta.x = sizeWidth;
        selfRectTrans.sizeDelta = sizeDelta;
        return selfRectTrans;
    }

    public static RectTransform SetSizeHeight(this RectTransform selfRectTrans, float sizeHeight)
    {
        var sizeDelta = selfRectTrans.sizeDelta;
        sizeDelta.y = sizeHeight;
        selfRectTrans.sizeDelta = sizeDelta;
        return selfRectTrans;
    }

    public static Vector2 GetWorldSize(this RectTransform selfRectTrans)
    {
        return RectTransformUtility.CalculateRelativeRectTransformBounds(selfRectTrans).size;
    }
}